package com.guoguo.controller;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;
import com.guoguo.base.PageBean;
import com.guoguo.entity.Purview;
import com.guoguo.service.IPurviewService;
import com.guoguo.utils.UUIDUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by KLSD on 2017/9/13.
 */
@Controller
@RequestMapping("/view")
public class PurviewController extends BaseController {
    @Autowired
    private IPurviewService iPurviewService;

    @RequestMapping(value = "/purviewList", method = RequestMethod.GET)
    public ModelAndView purview(@RequestParam int pageNumber,@RequestParam int pageSize) {
        Map<String, Object> data = new HashedMap();
        PageBean<Purview> purview = iPurviewService.selectPurviewList(pageNumber,pageSize);
        data.put("purviewList", purview);
        return returnView("/purviewList", data);


    }


    /**
     * 跳转新增修改权限
     *
     * @param purviewId
     * @return
     */
    @RequestMapping(value = "/savePurview", method = RequestMethod.GET)
    public ModelAndView savePurview(String purviewId) {
        Map<String, Object> data = new HashedMap();
        if ("".equals(purviewId) || purviewId == null) {
            data.put("purviewId", UUIDUtils.getUUID());
        }else {
            Purview purview = iPurviewService.selectByPrimaryKey(purviewId);
            data.put("purviewId", purview.getPurviewId());
            data.put("purviewName", purview.getPurviewName());
            data.put("purviewDescription", purview.getPurviewDescription());
            data.put("url", purview.getUrl());
            data.put("permission", purview.getPermission());
            data.put("status",purview.isStatus());
        }
        return returnView("/purview/purviewAdd", data);

    }

    @ResponseBody
    @RequestMapping(value = "/saveOrupdatePurview", method = RequestMethod.POST)
    public JSONObject updatePurview(@RequestParam String purviewId, @RequestParam String purviewName, @RequestParam String purviewDescription, @RequestParam String url, @RequestParam String permission,@RequestParam Boolean status) {
        Purview purview = iPurviewService.addPurview(purviewId, purviewName, purviewDescription, url, permission,status);
        return returnResult(200, purview, "");
    }
}
